{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.3 \u7ebf\u7a0b\u95f4\u901a\u4fe1\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u4e2d\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u4f60\u9700\u8981\u5728\u8fd9\u4e9b\u7ebf\u7a0b\u4e4b\u95f4\u5b89\u5168\u5730\u4ea4\u6362\u4fe1\u606f\u6216\u6570\u636e"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ece\u4e00\u4e2a\u7ebf\u7a0b\u5411\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53d1\u9001\u6570\u636e\u6700\u5b89\u5168\u7684\u65b9\u5f0f\u53ef\u80fd\u5c31\u662f\u4f7f\u7528 queue \u5e93\u4e2d\u7684\u961f\u5217\u4e86\u3002\u521b\u5efa\u4e00\u4e2a\u88ab\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u7684 Queue \u5bf9\u8c61\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u901a\u8fc7\u4f7f\u7528 put() \u548c get() \u64cd\u4f5c\u6765\u5411\u961f\u5217\u4e2d\u6dfb\u52a0\u6216\u8005\u5220\u9664\u5143\u7d20\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from queue import Queue\nfrom threading import Thread\n\n# A thread that produces data\ndef producer(out_q):\n    while True:\n        # Produce some data\n        ...\n        out_q.put(data)\n\n# A thread that consumes data\ndef consumer(in_q):\n    while True:\n# Get some data\n        data = in_q.get()\n        # Process the data\n        ...\n\n# Create the shared queue and launch both threads\nq = Queue()\nt1 = Thread(target=consumer, args=(q,))\nt2 = Thread(target=producer, args=(q,))\nt1.start()\nt2.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Queue \u5bf9\u8c61\u5df2\u7ecf\u5305\u542b\u4e86\u5fc5\u8981\u7684\u9501\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u5728\u591a\u4e2a\u7ebf\u7a0b\u95f4\u591a\u5b89\u5168\u5730\u5171\u4eab\u6570\u636e\u3002\n\u5f53\u4f7f\u7528\u961f\u5217\u65f6\uff0c\u534f\u8c03\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u5173\u95ed\u95ee\u9898\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u9ebb\u70e6\u3002\u4e00\u4e2a\u901a\u7528\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u961f\u5217\u4e2d\u653e\u7f6e\u4e00\u4e2a\u7279\u6b8a\u7684\u503c\uff0c\u5f53\u6d88\u8d39\u8005\u8bfb\u5230\u8fd9\u4e2a\u503c\u7684\u65f6\u5019\uff0c\u7ec8\u6b62\u6267\u884c\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from queue import Queue\nfrom threading import Thread\n\n# Object that signals shutdown\n_sentinel = object()\n\n# A thread that produces data\ndef producer(out_q):\n    while running:\n        # Produce some data\n        ...\n        out_q.put(data)\n\n    # Put the sentinel on the queue to indicate completion\n    out_q.put(_sentinel)\n\n# A thread that consumes data\ndef consumer(in_q):\n    while True:\n        # Get some data\n        data = in_q.get()\n\n        # Check for termination\n        if data is _sentinel:\n            in_q.put(_sentinel)\n            break\n\n        # Process the data\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u4f8b\u4e2d\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5730\u65b9\uff1a\u6d88\u8d39\u8005\u5728\u8bfb\u5230\u8fd9\u4e2a\u7279\u6b8a\u503c\u4e4b\u540e\u7acb\u5373\u53c8\u628a\u5b83\u653e\u56de\u5230\u961f\u5217\u4e2d\uff0c\u5c06\u4e4b\u4f20\u9012\u4e0b\u53bb\u3002\u8fd9\u6837\uff0c\u6240\u6709\u76d1\u542c\u8fd9\u4e2a\u961f\u5217\u7684\u6d88\u8d39\u8005\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u5168\u90e8\u5173\u95ed\u4e86\u3002\n\u5c3d\u7ba1\u961f\u5217\u662f\u6700\u5e38\u89c1\u7684\u7ebf\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u4ee5\u81ea\u5df1\u901a\u8fc7\u521b\u5efa\u81ea\u5df1\u7684\u6570\u636e\u7ed3\u6784\u5e76\u6dfb\u52a0\u6240\u9700\u7684\u9501\u548c\u540c\u6b65\u673a\u5236\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u901a\u4fe1\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Condition \u53d8\u91cf\u6765\u5305\u88c5\u4f60\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e0b\u8fb9\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u4f18\u5148\u7ea7\u961f\u5217\uff0c\u5982\u540c1.5\u8282\u4e2d\u4ecb\u7ecd\u7684\u90a3\u6837\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import heapq\nimport threading\n\nclass PriorityQueue:\n    def __init__(self):\n        self._queue = []\n        self._count = 0\n        self._cv = threading.Condition()\n    def put(self, item, priority):\n        with self._cv:\n            heapq.heappush(self._queue, (-priority, self._count, item))\n            self._count += 1\n            self._cv.notify()\n\n    def get(self):\n        with self._cv:\n            while len(self._queue) == 0:\n                self._cv.wait()\n            return heapq.heappop(self._queue)[-1]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u961f\u5217\u6765\u8fdb\u884c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u662f\u4e00\u4e2a\u5355\u5411\u3001\u4e0d\u786e\u5b9a\u7684\u8fc7\u7a0b\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u6ca1\u6709\u529e\u6cd5\u77e5\u9053\u63a5\u6536\u6570\u636e\u7684\u7ebf\u7a0b\u662f\u4ec0\u4e48\u65f6\u5019\u63a5\u6536\u5230\u7684\u6570\u636e\u5e76\u5f00\u59cb\u5de5\u4f5c\u7684\u3002\u4e0d\u8fc7\u961f\u5217\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u5b8c\u6210\u7684\u7279\u6027\uff0c\u6bd4\u5982\u4e0b\u8fb9\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684 task_done() \u548c join() \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from queue import Queue\nfrom threading import Thread\n\n# A thread that produces data\ndef producer(out_q):\n    while running:\n        # Produce some data\n        ...\n        out_q.put(data)\n\n# A thread that consumes data\ndef consumer(in_q):\n    while True:\n        # Get some data\n        data = in_q.get()\n\n        # Process the data\n        ...\n        # Indicate completion\n        in_q.task_done()\n\n# Create the shared queue and launch both threads\nq = Queue()\nt1 = Thread(target=consumer, args=(q,))\nt2 = Thread(target=producer, args=(q,))\nt1.start()\nt2.start()\n\n# Wait for all produced items to be consumed\nq.join()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u9700\u8981\u5728\u4e00\u4e2a\u201c\u6d88\u8d39\u8005\u201d\u7ebf\u7a0b\u5904\u7406\u5b8c\u7279\u5b9a\u7684\u6570\u636e\u9879\u65f6\u7acb\u5373\u5f97\u5230\u901a\u77e5\uff0c\u4f60\u53ef\u4ee5\u628a\u8981\u53d1\u9001\u7684\u6570\u636e\u548c\u4e00\u4e2a Event \u653e\u5230\u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u6837\u201c\u751f\u4ea7\u8005\u201d\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2aEvent\u5bf9\u8c61\u6765\u76d1\u6d4b\u5904\u7406\u7684\u8fc7\u7a0b\u4e86\u3002\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from queue import Queue\nfrom threading import Thread, Event\n\n# A thread that produces data\ndef producer(out_q):\n    while running:\n        # Produce some data\n        ...\n        # Make an (data, event) pair and hand it to the consumer\n        evt = Event()\n        out_q.put((data, evt))\n        ...\n        # Wait for the consumer to process the item\n        evt.wait()\n\n# A thread that consumes data\ndef consumer(in_q):\n    while True:\n        # Get some data\n        data, evt = in_q.get()\n        # Process the data\n        ...\n        # Indicate completion\n        evt.set()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u57fa\u4e8e\u7b80\u5355\u961f\u5217\u7f16\u5199\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u4e00\u4e2a\u6bd4\u8f83\u660e\u667a\u7684\u9009\u62e9\u3002\u4ece\u7ebf\u7a0b\u5b89\u5168\u961f\u5217\u7684\u5e95\u5c42\u5b9e\u73b0\u6765\u770b\uff0c\u4f60\u65e0\u9700\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u9501\u548c\u5176\u4ed6\u5e95\u5c42\u7684\u540c\u6b65\u673a\u5236\uff0c\u8fd9\u4e9b\u53ea\u4f1a\u628a\u4f60\u7684\u7a0b\u5e8f\u5f04\u5f97\u4e71\u4e03\u516b\u7cdf\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u961f\u5217\u8fd9\u79cd\u57fa\u4e8e\u6d88\u606f\u7684\u901a\u4fe1\u673a\u5236\u53ef\u4ee5\u88ab\u6269\u5c55\u5230\u66f4\u5927\u7684\u5e94\u7528\u8303\u7574\uff0c\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u628a\u4f60\u7684\u7a0b\u5e8f\u653e\u5165\u591a\u4e2a\u8fdb\u7a0b\u751a\u81f3\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u800c\u65e0\u9700\u6539\u53d8\u5e95\u5c42\u7684\u961f\u5217\u7ed3\u6784\u3002\n\u4f7f\u7528\u7ebf\u7a0b\u961f\u5217\u6709\u4e00\u4e2a\u8981\u6ce8\u610f\u7684\u95ee\u9898\u662f\uff0c\u5411\u961f\u5217\u4e2d\u6dfb\u52a0\u6570\u636e\u9879\u65f6\u5e76\u4e0d\u4f1a\u590d\u5236\u6b64\u6570\u636e\u9879\uff0c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u5b9e\u9645\u4e0a\u662f\u5728\u7ebf\u7a0b\u95f4\u4f20\u9012\u5bf9\u8c61\u5f15\u7528\u3002\u5982\u679c\u4f60\u62c5\u5fc3\u5bf9\u8c61\u7684\u5171\u4eab\u72b6\u6001\uff0c\u90a3\u4f60\u6700\u597d\u53ea\u4f20\u9012\u4e0d\u53ef\u4fee\u6539\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\uff1a\u6574\u578b\u3001\u5b57\u7b26\u4e32\u6216\u8005\u5143\u7ec4\uff09\u6216\u8005\u4e00\u4e2a\u5bf9\u8c61\u7684\u6df1\u62f7\u8d1d\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from queue import Queue\nfrom threading import Thread\nimport copy\n\n# A thread that produces data\ndef producer(out_q):\n    while True:\n        # Produce some data\n        ...\n        out_q.put(copy.deepcopy(data))\n\n# A thread that consumes data\ndef consumer(in_q):\n    while True:\n        # Get some data\n        data = in_q.get()\n        # Process the data\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Queue \u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e9b\u5728\u5f53\u524d\u4e0a\u4e0b\u6587\u5f88\u6709\u7528\u7684\u9644\u52a0\u7279\u6027\u3002\u6bd4\u5982\u5728\u521b\u5efa Queue \u5bf9\u8c61\u65f6\u63d0\u4f9b\u53ef\u9009\u7684 size \u53c2\u6570\u6765\u9650\u5236\u53ef\u4ee5\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002\u5bf9\u4e8e\u201c\u751f\u4ea7\u8005\u201d\u4e0e\u201c\u6d88\u8d39\u8005\u201d\u901f\u5ea6\u6709\u5dee\u5f02\u7684\u60c5\u51b5\uff0c\u4e3a\u961f\u5217\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u6dfb\u52a0\u4e0a\u9650\u662f\u6709\u610f\u4e49\u7684\u3002\u6bd4\u5982\uff0c\u4e00\u4e2a\u201c\u751f\u4ea7\u8005\u201d\u4ea7\u751f\u9879\u76ee\u7684\u901f\u5ea6\u6bd4\u201c\u6d88\u8d39\u8005\u201d \u201c\u6d88\u8d39\u201d\u7684\u901f\u5ea6\u5feb\uff0c\u90a3\u4e48\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u961f\u5217\u5c31\u53ef\u4ee5\u5728\u961f\u5217\u5df2\u6ee1\u7684\u65f6\u5019\u963b\u585e\u961f\u5217\uff0c\u4ee5\u514d\u672a\u9884\u671f\u7684\u8fde\u9501\u6548\u5e94\u6269\u6563\u6574\u4e2a\u7a0b\u5e8f\u9020\u6210\u6b7b\u9501\u6216\u8005\u7a0b\u5e8f\u8fd0\u884c\u5931\u5e38\u3002\u5728\u901a\u4fe1\u7684\u7ebf\u7a0b\u4e4b\u95f4\u8fdb\u884c\u201c\u6d41\u91cf\u63a7\u5236\u201d\u662f\u4e00\u4e2a\u770b\u8d77\u6765\u5bb9\u6613\u5b9e\u73b0\u8d77\u6765\u56f0\u96be\u7684\u95ee\u9898\u3002\u5982\u679c\u4f60\u53d1\u73b0\u81ea\u5df1\u66fe\u7ecf\u8bd5\u56fe\u901a\u8fc7\u6446\u5f04\u961f\u5217\u5927\u5c0f\u6765\u89e3\u51b3\u4e00\u4e2a\u95ee\u9898\uff0c\u8fd9\u4e5f\u8bb8\u5c31\u6807\u5fd7\u7740\u4f60\u7684\u7a0b\u5e8f\u53ef\u80fd\u5b58\u5728\u8106\u5f31\u8bbe\u8ba1\u6216\u8005\u56fa\u6709\u7684\u53ef\u4f38\u7f29\u95ee\u9898\u3002\nget() \u548c put() \u65b9\u6cd5\u90fd\u652f\u6301\u975e\u963b\u585e\u65b9\u5f0f\u548c\u8bbe\u5b9a\u8d85\u65f6\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import queue\nq = queue.Queue()\n\ntry:\n    data = q.get(block=False)\nexcept queue.Empty:\n    ...\n\ntry:\n    q.put(item, block=False)\nexcept queue.Full:\n    ...\n\ntry:\n    data = q.get(timeout=5.0)\nexcept queue.Empty:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u53ef\u4ee5\u7528\u6765\u907f\u514d\u5f53\u6267\u884c\u67d0\u4e9b\u7279\u5b9a\u961f\u5217\u64cd\u4f5c\u65f6\u53d1\u751f\u65e0\u9650\u963b\u585e\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\uff0c\u4e00\u4e2a\u975e\u963b\u585e\u7684 put() \u65b9\u6cd5\u548c\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u961f\u5217\u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u6837\u5f53\u961f\u5217\u5df2\u6ee1\u65f6\u5c31\u53ef\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u3002\u6bd4\u5982\u8f93\u51fa\u4e00\u6761\u65e5\u5fd7\u4fe1\u606f\u5e76\u4e22\u5f03\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def producer(q):\n    ...\n    try:\n        q.put(item, block=False)\n    except queue.Full:\n        log.warning('queued item %r discarded!', item)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u56fe\u8ba9\u6d88\u8d39\u8005\u7ebf\u7a0b\u5728\u6267\u884c\u50cf q.get() \u8fd9\u6837\u7684\u64cd\u4f5c\u65f6\uff0c\u8d85\u65f6\u81ea\u52a8\u7ec8\u6b62\u4ee5\u4fbf\u68c0\u67e5\u7ec8\u6b62\u6807\u5fd7\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528 q.get() \u7684\u53ef\u9009\u53c2\u6570 timeout \uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "_running = True\n\ndef consumer(q):\n    while _running:\n        try:\n            item = q.get(timeout=5.0)\n            # Process item\n            ...\n        except queue.Empty:\n            pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u6709 q.qsize() \uff0c q.full() \uff0c q.empty() \u7b49\u5b9e\u7528\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u961f\u5217\u7684\u5f53\u524d\u5927\u5c0f\u548c\u72b6\u6001\u3002\u4f46\u8981\u6ce8\u610f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u53ef\u80fd\u4f60\u5bf9\u4e00\u4e2a\u961f\u5217\u4f7f\u7528 empty() \u5224\u65ad\u51fa\u8fd9\u4e2a\u961f\u5217\u4e3a\u7a7a\uff0c\u4f46\u540c\u65f6\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u5df2\u7ecf\u5411\u8fd9\u4e2a\u961f\u5217\u4e2d\u63d2\u5165\u4e00\u4e2a\u6570\u636e\u9879\u3002\u6240\u4ee5\uff0c\u4f60\u6700\u597d\u4e0d\u8981\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}